package cn.ibizlab.core.open.justauth;

import cn.ibizlab.util.common.ConfigParser;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.request.AuthDefaultRequest;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.Map;

public class AuthCustomSource implements AuthSource {

    private String authorizeUrl;
    private String accessTokenUrl;
    private String accessTokenMethod = "POST";
    private String userInfoUrl;
    private String userInfoMethod = "GET";
    private String revokeUrl;
    private String refreshUrl;
    private String name;
    private Class<? extends AuthDefaultRequest> targetClass;

    private String mappingConfig;
    private String successFlag;



    public AuthCustomSource setAuthorizeUrl(String authorizeUrl) {
        this.authorizeUrl = authorizeUrl;
        return this;
    }

    public AuthCustomSource setAccessTokenUrl(String accessTokenUrl) {
        this.accessTokenUrl = accessTokenUrl;
        return this;
    }

    public String getAccessTokenMethod() {
        return this.accessTokenMethod;
    }

    public AuthCustomSource setAccessTokenMethod(String accessTokenMethod) {
        if(accessTokenMethod!=null)
            this.accessTokenMethod = accessTokenMethod;
        return this;
    }

    public String getUserInfoMethod() {
        return this.userInfoMethod;
    }

    public AuthCustomSource setUserInfoMethod(String userInfoMethod) {
        if(accessTokenMethod!=null)
            this.userInfoMethod = userInfoMethod;
        return this;
    }

    public AuthCustomSource setUserInfoUrl(String userInfoUrl) {
        this.userInfoUrl = userInfoUrl;
        return this;
    }

    public AuthCustomSource setRevokeUrl(String revokeUrl) {
        this.revokeUrl = revokeUrl;
        return this;
    }

    public AuthCustomSource setRefreshUrl(String refreshUrl) {
        this.refreshUrl = refreshUrl;
        return this;
    }

    public AuthCustomSource setName(String name) {
        this.name = name;
        return this;
    }

    public AuthCustomSource setTargetClass(Class<? extends AuthDefaultRequest> targetClass) {
        this.targetClass = targetClass;
        return this;
    }

    public String getMappingConfig() {
        return this.mappingConfig;
    }

    public AuthCustomSource setMappingConfig(String mappingConfig) {
        this.mappingConfig = mappingConfig;
        return this;
    }

    public Map<String,Object> getMapping() {
        if(ObjectUtils.isEmpty(this.getMappingConfig()))
            return Collections.emptyMap();
        return ConfigParser.parseConfig(this.getMappingConfig());
    }

    public String getSuccessFlag() {
        return this.successFlag;
    }

    public AuthCustomSource setSuccessFlag(String successFlag) {
        this.successFlag = successFlag;
        return this;
    }

    public Map<String,Object> getSuccessFlagMapping() {
        if(ObjectUtils.isEmpty(this.getSuccessFlag()))
            return Collections.emptyMap();
        return ConfigParser.parseConfig(this.getSuccessFlag());
    }

    @Override
    public String authorize() {
        return this.authorizeUrl;
    }

    @Override
    public String accessToken() {
        return this.accessTokenUrl;
    }

    @Override
    public String userInfo() {
        return this.userInfoUrl;
    }

    @Override
    public String revoke() {
        return this.revokeUrl;
    }

    @Override
    public String refresh() {
        return this.refreshUrl;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public Class<? extends AuthDefaultRequest> getTargetClass() {
        return this.targetClass;
    }

    @Override
    public String toString() {
        return this.name==null?super.toString():this.name;
    }
}
